<template>
	<view class="">
		
	</view>
</template>

<script setup>
import { onMounted, defineProps, defineExpose } from 'vue';
import {onUnload } from '@dcloudio/uni-app'
const props = defineProps({
	serviceId:{
		type:Number,
		default:0
	},
})
const showPanel = () =>{
	_MEIQIA('showPanel');
}
// 暴露 showPanel 方法给父组件使用
defineExpose({
  showPanel
});
// 获取 IP 地址并查询其所属地
function getIpLocation() {
  return new Promise((resolve, reject) => {
    // 主请求：尝试访问 ip-api.com
    fetch('http://ip-api.com/json')
      .then(response => response.json())
      .then(data => {
        if (data.status === 'fail') {
          // 主请求失败，切换到备用方案
          return tryBackupServices(resolve, reject);
        }
        resolve(data); // 成功获取数据
      })
      .catch(() => {
        // 主请求异常，切换到备用方案
        tryBackupServices(resolve, reject);
      });
  });
}

// 备用方案数组
const backupServices = [
  // { url: 'https://ipinfo.io/json?token=YOUR_TOKEN', parser: data => ({ city: data.city, region: data.region }) },
  { url: 'https://ipwhois.app/json/', parser: data => ({ city: data.city, region: data.region }) },
  // { url: 'http://api.ipstack.com/check?access_key=YOUR_ACCESS_KEY', parser: data => ({ city: data.city, region: data.region_name }) },
  { url: 'https://json.geoiplookup.io/', parser: data => ({ city: data.city, region: data.region_name }) }
];

// 遍历备用服务逐一尝试
function tryBackupServices(resolve, reject) {
  let index = 0;

  const tryNext = () => {
    if (index >= backupServices.length) {
      reject('所有备用 IP 查询服务均失败');
      return;
    }

    const service = backupServices[index];
    index += 1;

    fetch(service.url)
      .then(response => response.json())
      .then(data => {
        if (data.city && data.region) {
          resolve(service.parser(data)); // 成功解析数据
        } else {
          tryNext(); // 当前服务无效，尝试下一个
        }
      })
      .catch(() => {
        tryNext(); // 请求失败，尝试下一个
      });
  };

  tryNext(); // 开始尝试第一个服务
}

const userInfo = uni.getStorageSync('USERINDEX');
// 注册监听
uni.$on('userInfoUpdated', (userInfo) => {
    // console.log('User info updated:', userInfo);
    // 更新页面中的数据
    userInfo = userInfo;
});

// 页面销毁时移除监听
	onUnload (()=>{
		uni.$off('userInfoUpdated');
	})

onMounted(() => {
  const msg = "账号：" + userInfo.username + ",姓名：" + userInfo.nickname + ",可用余额：" + userInfo.money;
  
  if (!window._MEIQIA) {
    (function(m, ei, q, i, a, j, s) {
      m[i] = m[i] || function() {
        (m[i].a = m[i].a || []).push(arguments);
      };
      j = ei.createElement(q);
      s = ei.getElementsByTagName(q)[0];
      j.async = true;
      j.charset = 'UTF-8';
      j.src = 'https://static.meiqia.com/dist/meiqia.js';
      s.parentNode.insertBefore(j, s);
    })(window, document, 'script', '_MEIQIA');
  }
  
  _MEIQIA('entId', '11481b85a7cf40ef76d5889c6779aa0a');
  _MEIQIA('withoutBtn');
  _MEIQIA('languageLocal', 'true');
  
	getIpLocation().then(location => {
		// 传递顾客信息
		_MEIQIA('metadata', {
		  address: location.city + ', ' + location.region,  // 传递 IP 地址所属地
		  name: msg,
		});
	}).catch(error => {
		console.error('获取 IP 地址失败:', error);
	});
});

</script>

<style>
</style>